package com.luli.service.impl;

/**
 * 陆离
 * 2022/5/24 17:58:29
 */

import com.luli.entity.MongoTest;
import com.luli.service.MongoTestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Slf4j
@Service
public class MongoTestServiceImpl implements MongoTestService {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 创建对象
     * @param mgtest
     */
    @Override
    public Integer saveTest(MongoTest mgtest) {
        MongoTest insert = mongoTemplate.insert(mgtest);
        log.info("新增{}数据",insert);
        Query query = Query.query(Criteria.where("name").is(mgtest.getNames()));
        if(!ObjectUtils.isEmpty(query)){
            return 1;
        }else {
            return 0;
        }
    }
}
